const CompressionPlugin = require('compression-webpack-plugin')
const {pages, outputDir, devServer} = require('./config/projectConfig');
const projectName = require('./config/projects');
const NODE_ENV = process.env.NODE_ENV;
const path = require('path');

function resolve(dir) {
    return path.join(__dirname, dir)
}

module.exports = {
    pages,
    outputDir,
    devServer,
    publicPath: NODE_ENV === 'production' ? './' : '/',
    css: {
        extract: true,
        sourceMap: false,
        loaderOptions: {
            sass: {
                prependData: `@import "~@/assets/styles/utils.scss";`
            }
        }
    },
    chainWebpack: config => {
        config.plugins.delete('preload') // 移除 preload 插件
        config.plugins.delete('prefetch') // 移除 prefetch 插件

        config.resolve.alias.set('#', resolve(`src/projects/${projectName.name}/src`)); // 定义在每个项目中的src路径



        // 下面代码是控制public的路径，否则public下的js无法被引用
        config.plugin('copy').use(require('copy-webpack-plugin'), [
            [
                {
                    from: `src/projects/${projectName.name}/public`,
                    to: './',
                    ignore: []
                }
            ]
        ]);
    },
    configureWebpack: (config) => {
        if (NODE_ENV === 'production') {
            // 为生产环境修改配置...
            config.mode = 'production'

            return {
                plugins: [
                    new CompressionPlugin({
                        filename: '[path].gz[query]',
                        algorithm: 'gzip',
                        test: /\.js$|\.html$|\.css/, //匹配文件名
                        threshold: 10240, //对超过10k的数据进行压缩
                        deleteOriginalAssets: false //是否删除原文件
                    })
                ]
            }
        }
    },
    productionSourceMap: false
}
